package com.hlj.example.notification;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    private DownloadThread mHandlerThread;//子线程
    private Handler mUIhandler;//主线程的Handler

    public static final int NOTIFICATION_ID = 10001;
    private NotificationManager notificationManager;
    private Notification.Builder builder;
    private Notification notification;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mHandlerThread = new DownloadThread("DownloadThread");
        mHandlerThread.start();
        mUIhandler = new Handler(mHandlerThread.getLooper()) {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case DownloadThread.TYPE_START:
                        initNotification();
                        break;
                    case DownloadThread.TYPE_FINISHED:
                        notificationManager.cancel(NOTIFICATION_ID);
                        break;
                    case DownloadThread.TYPE_DOWNLOADING:
                        notification = builder
                                .setContentText(String.valueOf(msg.arg1))
                                .setProgress(100, msg.arg1, false)
                                .build();
                        notificationManager.notify(NOTIFICATION_ID, notification);
                        break;
                    default:
                        break;
                }
            }
        };
        mHandlerThread.setUIHandler(mUIhandler);
        mHandlerThread.startDownload();

    }

    private void initNotification() {
        notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(getPackageName(), getPackageName(), NotificationManager.IMPORTANCE_DEFAULT);
            channel.setSound(null, null);
            channel.enableLights(false);
            channel.enableVibration(false);
            notificationManager.createNotificationChannel(channel);
            builder = new Notification.Builder(this, getPackageName());
        } else {
            builder = new Notification.Builder(this);
        }
        builder.setContentTitle("Title");
        builder.setContentText("0");
        builder.setProgress(100, 0, false);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        notification = builder.build();
        notificationManager.notify(NOTIFICATION_ID, notification);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mHandlerThread.quit();
    }

}
